package movee.spring.app.template.domain.dao;

import com.fasterxml.jackson.annotation.JsonIdentityInfo;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.ObjectIdGenerators;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import lombok.ToString;
import lombok.experimental.SuperBuilder;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Index;
import javax.persistence.OneToMany;
import javax.persistence.OrderBy;
import javax.persistence.Table;
import java.io.Serializable;
import java.util.LinkedList;
import java.util.List;

/**
 *
 *
 * @author movee
 */
@Data
@NoArgsConstructor
@ToString(callSuper = true)
@EqualsAndHashCode(callSuper = true)
@SuperBuilder
@DynamicUpdate
@DynamicInsert
@Entity
@Table(name = "t_order", indexes = {
        @Index(name = "idx_name", columnList = "name"),
        @Index(name = "idx_creator", columnList = "creator"),
        @Index(name = "idx_updater", columnList = "updater"),
        @Index(name = "idx_create_time", columnList = "create_time"),
        @Index(name = "idx_update_time", columnList = "update_time")
})
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id",
        scope = JoinedOrderDao.class)
@JsonIgnoreProperties(value = { "hibernateLazyInitializer", "handler" })
public class JoinedOrderDao extends BaseOrderDao implements Serializable {

    /**
     * 关联的item（一对多）
     */
    @OneToMany(fetch = FetchType.LAZY, mappedBy = "order", cascade = CascadeType.ALL)
    @OrderBy("sequence ASC")
    private List<JoinedItemDao> items = new LinkedList<>();

}

